package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.FootprintMapper;
import com.cskaoyan.model.Footprint;
import com.cskaoyan.model.FootprintExample;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.DataVO;
import com.cskaoyan.model.vo.WxFootPrintVO;
import com.cskaoyan.service.FootprintService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @description：实现会员足迹部分具体操作
 * @author     ：huzhouming
 * @date       ：2022/5/6 14:08
 */
@Transactional
@Service
public class FootprintServiceImpl implements FootprintService {
    @Autowired
    FootprintMapper footprintMapper;

    @Transactional
    @Override
    /**
     * @author: huzhouming
     * @description: 查询会员足迹信息并分页
     * @date: 2022/5/6
     * @param paramBO
     * @param userId
     * @param goodsId
     * @return com.cskaoyan.model.vo.DataVO<com.cskaoyan.model.Footprint>
     */
    public DataVO<Footprint> query(BaseParamBO paramBO, Integer userId, Integer goodsId) {
        Integer page = paramBO.getPage();//页码
        Integer limit = paramBO.getLimit();//数据量
        String sort = paramBO.getSort();//排序列
        String order = paramBO.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息

        FootprintExample footprintExample = new FootprintExample();
        FootprintExample.Criteria criteria = footprintExample.createCriteria();
        if (userId != null ) {
            criteria.andUserIdEqualTo(userId);
        }
        if (goodsId != null) {
            criteria.andGoodsIdEqualTo(goodsId);
        }
        criteria.andDeletedEqualTo(false);
        footprintExample.setOrderByClause(sort + " " + order);

        List<Footprint> list = footprintMapper.selectByExample(footprintExample);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<Footprint> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return DataVO.data(total, pages, limit, page, list);
    }

    /**
     * @author: zhoukang
     * @description: TODO 查询用户足迹(deleted = 0)
     * @date: 2022/5/7 22:40
     */
    @Override
    public DataVO<WxFootPrintVO> queryUserFootprint(Integer userId,BaseParamBO paramBO) {
        Integer limit = paramBO.getLimit();
        Integer page = paramBO.getPage();

        //排序列为updateTime
        paramBO.setSort("update_time");
        //排序方式为desc
        paramBO.setOrder("desc");
        //分页插件
        PageHelper.startPage(page, limit);

        List<WxFootPrintVO> list = footprintMapper.selectFootprintByUserId(userId,paramBO.getOrder(),paramBO.getSort());
        PageInfo<WxFootPrintVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return DataVO.data(total, pages, limit, page, list);
    }

    /**
     * @author: zhoukang
     * @description: TODO 逻辑删除:选择性update-->根据id修改deleted
     * @date: 2022/5/8 16:10
     */
    @Override
    public void deleteFootprint(Footprint footprint) {
        footprintMapper.updateByPrimaryKeySelective(footprint);
    }
}
